// ==UserScript==
// @name         工具
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  提供些便捷功能:1. bilibili视频默认1.5倍速
// @author       You
// @match        *://*.bilibili.com/video/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=bilibili.com
// @run-at       document-idle
// @grant        none
// ==/UserScript==

(function () {
  'use strict';
  // @require      file:\\\H:\project\my-tampermonkey-rep\mytools.js
  window.addEventListener('load', function () {
    let speed = '1.5'
    let count = 5
    let timeId = null
    callMaker()
    function callMaker() {
      let $playbackrate_result = document.querySelector('.bpx-player-ctrl-playbackrate-result')
      if (!$playbackrate_result) {
        if (count > 0) {
          timeId = setTimeout(() => {
            count--
            callMaker()
          }, 3e3);
        } else {
          clearTimeout(timeId)
        }
      } else {
        if (speed != '1.0' && speed != $playbackrate_result.innerText.trim()) {
          let $playbackrate_menu_items = document.querySelectorAll('.bpx-player-ctrl-playbackrate-menu-item')
          $playbackrate_menu_items.forEach(node => {
            if (node.dataset.value == speed) {
              node.click()
            }
          })
        }
        clearTimeout(timeId)
      }
    }
  })
})();